import Vue from 'vue'
import Vuex from 'vuex'
import BasicConfig from "@/utils/config"
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex)

const modulesFiles = require.context('./modules', false, /\.js$/)
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
	// set './app.js' => 'app'
	const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
	const value = modulesFiles(modulePath)
	modules[moduleName] = value.default
	return modules
}, {})

const stroe = new Vuex.Store({
	modules,
	plugins: [
		createPersistedState({
			key: `__${BasicConfig.APPKEY}__`,
			paths: [...Object.keys(modules)],
			storage: {
				getItem: key => uni.getStorageSync(key),
				setItem: (key, val) => uni.setStorageSync(key, val),
				removeItem: key => uni.removeStorageSync(key)
			}
		})
	]
})

export default stroe
